$(function () {
    //定义原始模板
    var template_html = '';
    template_html += '<div class="output-view">';
    template_html += '<span class="prompt"><%= separate %></span>&nbsp;';
    template_html += '<span class="output<%= error %>"><%= value %></span>';
    template_html += '</div>';
    var template_output = _.template(template_html);
    //命令缓存
    var cmd_cache = [];
    var cmd_pos = 0;
    var $left = $('.left');
    var $right = $('.right');
    var $cursor = $('.cursor');
    var $shell = $('.shell-view');
    var $input = $('.input');
    var str_left = '';
    var str_cursor = '';
    var str_right = '';
    var str_tmp_cursor = '';
    var flag_end = false;

    //光标闪烁效果
    setInterval(function () {
        $cursor.toggleClass('blink');
    }, 1000);

    //监听键盘事件
    $(document).keypress(function (e) {
        if (e.which === 32) {
            //space
            $left.append('&nbsp;');
        }
        if (e.which !== 13) {
            // enter
            $left.append(String.fromCharCode(e.which));
            goBottom();
        }
    });

    // 功能键
    $(document).keydown(function (e) {
        var keyCode = e.which;
        switch (keyCode) {
            case 8:
                //backspace
                e.preventDefault();
                str_left = $left.text();
                if (str_left.length === 0) {
                    return;
                }
                str_left = str_left.substring(0, str_left.length - 1);
                $left.text(str_left);
                break;
            case 13:
                //enter
                var cmd = $.trim($input.text());
                var val_ouput = '';
                var err_class = '';
                var is_print = true;

                if (cmd !== '') {
                    cmd_cache.push(cmd);
                    cmd_cache = _.uniq(cmd_cache);
                }
                if (cmd_cache.length > 0) {
                    cmd_pos = cmd_cache.length - 1;
                }

                if (cmd === 'help') {
                    val_ouput = 'Type JavaScript syntax for interactive console, or type "clear" to clear terminal.';
                } else if (cmd === 'clear') {
                    $shell.siblings().remove();
                    is_print = false;
                } else {
                    try {
                        val_ouput = eval(cmd);
                    } catch (e) {
                        val_ouput = '\'' + cmd + '\': command not found';
                        err_class = ' error';
                    }
                }


                $left.text('');
                $cursor.html('&nbsp;');
                $right.text('');

                if (is_print) {
                    $shell.before(template_output({separate: '$', value: cmd, error: ''}));
                    $shell.before(template_output({separate: '&gt;', value: val_ouput, error: err_class}) + '<br />');
                }

                goBottom();
                break;
            case 35:
                //end
                str_right = $right.text();
                str_cursor = $cursor.text();
                var str_all = $input.text();

                if (str_right.length === 0 && $.trim(str_cursor).length === 0) {
                    return;
                }
                $left.text(str_all);
                $cursor.html('&nbsp;');
                $right.text('');

                break;
            case 36:
                //home
                str_left = $left.text();
                var str_all = $input.text();

                if (str_left.length === 0) {
                    return;
                }
                $left.text('');
                $cursor.text(str_all.substring(0, 1));
                $right.text(str_all.substring(1, str_all.length));
                break;
            case 37:
                //向左方向键
                str_left = $left.text();
                str_right = $right.text();
                str_cursor = $cursor.text();
                str_tmp_cursor = '';

                if (str_left.length === 0) {
                    return;
                }
                str_tmp_cursor = str_left.substring(str_left.length - 1, str_left.length);
                str_left = str_left.substring(0, str_left.length - 1);
                if (!($cursor.html() === '&nbsp;' && str_right.length === 0 && $.trim(str_tmp_cursor) !== '')) {
                    str_right = str_cursor + str_right;
                }

                $left.text(str_left);
                $cursor.text(str_tmp_cursor);
                $right.text(str_right);
                break;
            case 38:
                //向上方向键
                if (cmd_pos < 0) {
                    return;
                }

                $left.text(cmd_cache[cmd_pos]);
                cmd_pos--;
                $cursor.html('&nbsp;');
                $right.text('');
                break;
            case 39:
                //向右方向键
                str_left = $left.text();
                str_right = $right.text();
                str_cursor = $cursor.text();
                flag_end = false;

                if (str_right.length === 0) {
                    if ($cursor.html() === '&nbsp;') {
                        return;
                    }
                    flag_end = true;
                }
                str_left += str_cursor;
                if (flag_end) {
                    $cursor.html('&nbsp;');
                    str_right = '';
                } else {
                    $cursor.text(str_right.substring(0, 1));
                    str_right = str_right.substring(1);
                }

                $left.text(str_left);
                $right.text(str_right);
                break;
            case 40:
                //向下方向键
                if (cmd_pos >= cmd_cache.length - 1) {
                    $left.text('');
                } else {
                    cmd_pos++;
                    $left.text(cmd_cache[cmd_pos]);
                }

                $cursor.html('&nbsp;');
                $right.text('');
                break;
            case 46:
                //delete
                str_right = $right.text();

                if (str_right.length === 0) {
                    if ($cursor.html() === '&nbsp;') {
                        return;
                    }
                    flag_end = true;
                }

                if (flag_end) {
                    $cursor.html('&nbsp;');
                } else {
                    $cursor.text(str_right.substring(0, 1));
                    $right.text(str_right.substring(1));
                }
                break;
        }
        if (e.which === 85 && e.ctrlKey) {
            // Ctrl + U
            e.preventDefault();
            $left.text('');
        }
        if (e.which === 76 && e.ctrlKey) {
            // Ctrl + L
            e.preventDefault();
            $shell.siblings().remove();
        }
    });
});


function goBottom() {
    var win = $("#panel-shell")[0];
    console.log(win.scrollHeight);
    console.log(win.scrollTop);
    win.scrollTop = win.scrollHeight;
    console.log(win.scrollHeight);
    console.log(win.scrollTop);
}